import 'package:automated_test/common/constant.dart';
import 'package:automated_test/common/db_helper.dart';
import 'package:automated_test/common/myLog.dart';
import 'package:automated_test/common/network.dart';
import 'package:flutter/material.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:scan/scan.dart';

class QRScanPage extends StatefulWidget {
  @override
  _QRScanPageState createState() => _QRScanPageState();
}

class _QRScanPageState extends State<QRScanPage> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        leading: IconButton(
          padding: EdgeInsets.all(Dimension.sp8),
          icon: Icon(
            Icons.arrow_back,
            size: Dimension.sp24,
          ),
          onPressed: () => Navigator.pop(context),
        ),
        title: Text(
          "扫一扫",
          style: TextStyle(fontSize: Dimension.sp24),
        ),
        centerTitle: true,
      ),
      body: QRScanContent(),
    );
  }
}

class QRScanContent extends StatefulWidget {
  @override
  _QRScanContentState createState() => _QRScanContentState();
}

class _QRScanContentState extends State<QRScanContent> {
  ScanController _scanController = ScanController();

  @override
  void initState() {
    MyLog.w("QRScanContent", "initState");
    super.initState();
  }

  @override
  void activate() {
    MyLog.w("QRScanContent", "activate");
    super.activate();
  }

  @override
  void deactivate() {
    MyLog.w("QRScanContent", "deactivate");
    super.deactivate();
  }

  @override
  void dispose() {
    MyLog.w("QRScanContent", "dispose");
    _scanController.pause();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      width: MediaQuery.of(context).size.width,
      height: MediaQuery.of(context).size.height -
          MediaQuery.of(context).padding.top -
          kToolbarHeight,
      child: Center(
        child: ScanView(
          controller: _scanController,
          onCapture: (value) async {
            //https://op.cytingchechang.com/operate_mobile_manage/#/identify?mac=BC:FF:4D:07:2B:7A
            MyLog.d("onCapture", "value: $value");
            if (!value.contains("mac")) {
              EasyLoading.showInfo("没有找到可以匹配的数据");
              _scanController.resume();
              return;
            }
            String url = value;
            String mac = url.split("mac=")[1];
            MyLog.d("onCapture", "mac: $mac");
            EasyLoading.instance.indicatorType = EasyLoadingIndicatorType.wave;
            EasyLoading.show(status: "正在查询中", dismissOnTap: true);
            var result = await Network.checkDuplicate(mac);
            var code = result["code"];
            var message = result["message"];

            ///TODO 打开注释
            // if (code == -1 || result["error"] != null) {
            // EasyLoading.dismiss();
            // await EasyLoading.showError(message ?? result["error"],
            //     dismissOnTap: true);
            // _scanController.resume();
            // } else {
            if (!await DbHelper.existsGate(mac)) {
              DbHelper.insertGate(mac);
            }
            MyLog.i("gates", "${await DbHelper.queryGates()}");
            await EasyLoading.showInfo(message,
                duration: Duration(seconds: 5), dismissOnTap: true);
            Navigator.pushNamed(context, "/GateTest",
                arguments: {"controller": _scanController, "value": mac});
            // }
          },
        ),
      ),
    );
  }
}
